package com.eu.ebpm.engine.express.validator;

import java.math.BigDecimal;

import com.eu.ebpm.engine.exception.WorkFlowException;
import com.eu.ebpm.engine.express.Variable;

/**
 * 
 * @author wangxiaohua
 *
 */
public class NumberTypeValidator implements TypeValidator<BigDecimal> {

	public boolean checkVariable(Variable<BigDecimal> variable, Object condition) 
	{
		return check(variable.getExpress(), variable.getValue(), new BigDecimal(condition.toString()));
	}
	
	public static boolean check(String express, BigDecimal expressValue, BigDecimal data) {

		if (express.equals("==") || express.equals("=")) {
			return data.compareTo(expressValue) == 0;
		} else if (express.equals(">")) {
			return data.compareTo(expressValue) > 0;
		} else if (express.equals("<")) {
			return data.compareTo(expressValue) < 0;
		} else if (express.equals(">=")) {
			return data.compareTo(expressValue) >= 0;
		} else if (express.equals("<=")) {
			return data.compareTo(expressValue) <= 0;
		} else if (express.equals("!=")) {
			return data.compareTo(expressValue) != 0;
		}

		throw new WorkFlowException(String.format("[表达式验证异常] express = %s, expressValue = %s, data = %s", express, expressValue, data));
	}

	public BigDecimal transform(Object value) {
		return new BigDecimal((String)value);
	}
	
	
	
	
	

}
